<template>
  <!-- :title="title" -->
  <el-dialog
    :title="{ add: '新增权限', edit: '编辑权限' }[mode]"
    :visible.sync="isShow"
    @close="closeEvent"
  >
    <el-form ref="addform" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="权限名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="权限标识" prop="code">
        <el-input v-model="form.code" />
      </el-form-item>
      <el-form-item label="权限描述" prop="description">
        <el-input v-model="form.description" />
      </el-form-item>
      <el-form-item label="企业可见" prop="enVisible">
        <el-switch
          v-model="form.enVisible"
          active-text="可见"
          inactive-text="不可见"
          active-value="0"
          inactive-value="1"
        />
      </el-form-item>
    </el-form>
    <template>
      <div style="text-align: center">
        <el-button @click="closeEvent">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import { sysPermissionPost, sysPermissionEdit } from '@/api/permission'
export default {
  name: '',
  data() {
    return {
      mode: 'add',
      isShow: false,
      form: {
        name: '',
        code: '',
        description: '',
        enVisible: '0',
        type: 1,
        pid: '0'
      },
      rules: {
        name: [
          { required: true, message: '请输入权限名称', trigger: 'change' }
        ],
        code: [
          { required: true, message: '请输入权限标识', trigger: 'change' }
        ],
        description: [
          { required: true, message: '请输入权限描述', trigger: 'change' }
        ],
        enVisible: [{ required: true, message: '必填', trigger: 'change' }]
      }
    }
  },
  computed: {
    // title() {
    //   return {
    //     add: '新增部门',
    //     edit: '编辑部门'
    //   }[this.mode]
    // }
  },
  created() {},
  methods: {
    closeEvent() {
      this.isShow = false
      this.$refs.addform.resetFields()
      this.form = {
        name: '',
        code: '',
        description: '',
        enVisible: '0',
        type: 1, // 1表示添加页面，2表示添加按钮
        pid: '0'
      }
    },
    submit() {
      /*
      新增：
      1.第一新增api
      2.导入api
      3.表单验证
      4.点击确定按钮表单验证通过后调用api
      5.调用成功后提示成功，关闭弹框，清空数据和表单验证，刷新父级列表数据
      */
      this.$refs.addform.validate(async(valid) => {
        if (valid) {
          if (this.mode === 'add') {
            await sysPermissionPost(this.form)
            this.$message.success('新增成功')
          } else if (this.mode === 'edit') {
            await sysPermissionEdit(this.form)
            this.$message.success('编辑成功')
          }
          this.closeEvent()
          this.$emit('getData')
        }
      })
      /*
    编辑：
    1打开弹框
    2.数据回显
        a:当前项能拿到所有数据，只需要深拷贝赋值到弹框组件
        b:如果拿不能到所有数据，调用接口获取所有编辑项数据
    3.弹框编辑定义一个变量区分场景
        新增和编辑场景都有相应值的改变
    4.调整标题
        调用表单验证
    5.定义编辑api
    6.分场景调用相应api
    */
    }
  }
}
</script>
<style lang="less" scoped></style>
